mui.init({
 
});

var old_back = mui.back;
mui.back = function(){
	var targetPageId = plus.storage.getItem(KeyCfg.targetPageId);	
	if(!targetPageId){
		mui.alert("参数：targetPageId 为 null ,无法跳转到下一步");
		return;
	}
	var targetPage = plus.webview.getWebviewById(targetPageId);
    mui.fire(targetPage,'reload',{});
    targetPage.show();
}

mui.plusReady(function() {
	
	(function load(){
		var userId = plus.storage.getItem(KeyCfg.userId);
		$("#userid").val(userId);
		var pars={userId:userId};
		var url = UrlConfig.mgrpro_load;
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		setVals(data);
		  	},ffn : function(err){
		  		mui.alert("获取用户名失败");
		  	}
		 });
		
	})();
	

 	var sub = CmwMcore.$('sub');
	MeventMgr.onTouchstart(sub,function(event,self){					
			if(!valid()) return;			
//			submitForm();
		});		
	
	/*表单验证*/
	function valid(){
		 	var guid = $("#userid").val();//userID
			var bankCardNumber = $("#bankCardNumber").val();//卡号
			var bankId = $("#bankCheck").val();//银行
			var bankCity = $("#bankCity").val();	//开户城市
			var bankName = $("#bankName").val();//开户行
			var payPassword = $("#payPassword").val();//支付密码
			var a = document.getElementById("bankCardNumber");
			if (bankCardNumber.trim() == null || bankCardNumber.trim().length == 0){
				mui.alert("卡号不能为空!");
				return false;
			}
			bankCardNumber = bankCardNumber.replace(/\s+/g,"");
			console.log(bankCardNumber.trim().length); 
			if(bankCardNumber.trim().length != 16 && bankCardNumber.trim().length != 24 && bankCardNumber.trim().length != 19){
				mui.alert("卡号长度不正确!");
				return false;
			}
			
			if (bankId == '0' ||bankId ==""){
				mui.alert("请选择卡类型!");
				return false;
			}
			
			if (bankCity.trim() == null || bankCity.trim().length == 0 ){
				mui.alert("开户城市不能为空!");
				return false;
			}
			
			if (!bankName ){
				mui.alert("开户行不能为空!");
				return false;
			}
			
			if (payPassword.trim() != null && payPassword.trim().length > 0){
				var param = {"payPassword": payPassword};
				var url=UrlConfig.mybank_checkPay;
				$.ajax({
					type : "POST",
					url : url,
					data : param,
					dataType : "jsonp",
					success : function(data) {
						data = JSON.parse(data);
						if (data.success){
							submitForm();
						}else{
							mui.alert(data.msg);
							return false;
						}
						
					}
				});
			}else{
				mui.alert("交易密码不能为空!");
				return false;
			}
		return true;
	}
	
	function submitForm(){
		var userId = plus.storage.getItem(KeyCfg.userId);
		var bankCardNumber = $("#bankCardNumber").val();//卡号
		var bankCheck = $("#bankCheck").val();//卡类型
		var bankCity = $("#bankCity").val();	//开户城市
		var strs= new Array();
		strs= bankCheck.split("_");
		var bankName = $("#bankName").val();//开户行
		bankCheck = strs[0];
		var bankId = strs[1];
		var url=UrlConfig.mybank_bankSave;
		var cfg = {pars :{realName:name,userId:userId}};
		var pars={guid:userId,bankCardNumber:bankCardNumber,bankCheck:bankCheck,
			bankId:bankId,bankCity:bankCity,bankName:bankName}
//		$.ajax(url, {  
//	        data : pars,  
//	        dataType : 'jsonp',  
//	        crossDomain: true,  
//	        success: function(data) {
//	        		data = JSON.parse(data);
//	        		if (data.success){
//	        			mui.alert(data.msg);
//	        			mui.back();
//	        			mui.fire(targetPage,'reload',{});
//				}else{
//					mui.alert(data.msg);
//				}
//	        }
//	    });
		CmwMcore.disabled('sub');
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){  	
		  		CmwMcore.enabled('sub');
	        		if (data.success){
	        			mui.alert(data.msg);
	        			forward();
				}else{
					mui.alert(data.msg);
				}
		  	},ffn : function(err){
		  		CmwMcore.enabled('sub');
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		 });
	}
	
	function forward(){
		mui.back();
//		var targetPageId = plus.storage.getItem(KeyCfg.targetPageId);
//		if(!targetPageId){
//			mui.alert("参数：targetPageId 为 null ,无法跳转到下一步");
//			return;
//		}
//		var targetPage = plus.webview.getWebviewById(targetPageId);
//	    mui.fire(targetPage,'reload',{});
//	    targetPage.show();
	}
	
	function setVals(data){
		var realName = data.realName;
		$("#userName").val(realName);
	}
	
});